<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>车牌检测系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }

        h1 {
            color: #333;
            text-align: center;
        }

        .upload-container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        .result-container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            display: none;
        }

        #imagePreview {
            max-width: 100%;
            margin-top: 15px;
            display: none;
            border: 1px solid #ddd;
        }

        #uploadBtn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-top: 10px;
        }

        #uploadBtn:hover {
            background-color: #45a049;
        }

        .loading {
            display: none;
            text-align: center;
            margin: 10px 0;
        }

        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #3498db;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }

        @keyframes spin {
            0% {
                transform: rotate(0deg);
            }
            100% {
                transform: rotate(360deg);
            }
        }

        .plate-result {
            margin-top: 15px;
            padding: 15px;
            background-color: #e9f7ef;
            border-radius: 4px;
            border-left: 4px solid #4CAF50;
        }

        .plate-text {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .plate-confidence {
            color: #666;
        }

        .error {
            color: #d32f2f;
            padding: 10px;
            background-color: #ffebee;
            border-radius: 4px;
        }

        .file-input-container {
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
<h1>车牌检测系统</h1>

<div class="upload-container">
    <h2>上传图片</h2>
    <div class="file-input-container">
        <input type="file" id="imageInput" accept="image/*">
    </div>
    <button id="uploadBtn">检测车牌</button>
    <div class="loading" id="loading">
        <div class="spinner"></div>
        <p>正在检测车牌...</p>
    </div>
    <img id="imagePreview" alt="预览图片">
</div>

<div class="result-container" id="resultContainer">
    <h2>检测结果</h2>
    <div id="results"></div>
</div>

<script>
    document.getElementById('imageInput').addEventListener('change', function (e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function (event) {
                const imgPreview = document.getElementById('imagePreview');
                imgPreview.src = event.target.result;
                imgPreview.style.display = 'block';

                // 隐藏之前的结果
                document.getElementById('resultContainer').style.display = 'none';
            }
            reader.readAsDataURL(file);
        }
    });

    document.getElementById('uploadBtn').addEventListener('click', async function () {
        const fileInput = document.getElementById('imageInput');
        const file = fileInput.files[0];

        if (!file) {
            alert('请先选择一张图片');
            return;
        }

        const loading = document.getElementById('loading');
        const resultContainer = document.getElementById('resultContainer');
        const resultsDiv = document.getElementById('results');

        // 显示加载动画
        loading.style.display = 'block';
        resultContainer.style.display = 'none';

        try {
            const formData = new FormData();
            formData.append('file', file);

            const response = await fetch('/detect', {
                method: 'POST',
                body: formData
            });

            if (!response.ok) {
                throw new Error(`服务器错误: ${response.status}`);
            }

            const data = await response.json();

            loading.style.display = 'none';
            resultContainer.style.display = 'block';

            // 显示结果
            resultsDiv.innerHTML = '';

            if (data.error) {
                resultsDiv.innerHTML = `<div class="error">错误: ${data.error}</div>`;
                return;
            }

            if (data.length > 0) {
                data.forEach((result, index) => {
                    const [text, confidence, plate_img_base64] = result;
                    const plateDiv = document.createElement('div');
                    plateDiv.className = 'plate-result';
                    plateDiv.innerHTML = `
                    <div class="plate-text">车牌 ${index + 1}: ${text}</div>
                    <div class="plate-confidence">置信度: ${(confidence * 100).toFixed(2)}%</div>
                    <img src="data:image/jpeg;base64,${plate_img_base64}" alt="车牌图片" style="max-width: 100%; margin-top: 10px;">
                `;
                    resultsDiv.appendChild(plateDiv);
                });
            } else {
                resultsDiv.innerHTML = '<div class="plate-result">未检测到车牌</div>';
            }
        } catch (error) {
            loading.style.display = 'none';
            resultsDiv.innerHTML = `<div class="error">错误: ${error.message}</div>`;
            resultContainer.style.display = 'block';
            console.error('检测错误:', error);
        }
    });

</script>
</body>
</html>